package xin.yangshuai.mybatisgeneratorweb.plugin;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.TopLevelClass;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.util.StringUtility;

import java.text.SimpleDateFormat;
import java.util.Properties;

/**
 * MyCommentGenerator
 *
 * @author shuai
 * @date 2021/10/18
 */
public class MyCommentGenerator extends DefaultCommentGenerator {

    private Properties properties1 = new Properties();
    private boolean suppressDate1 = false;
    private boolean suppressAllComments1 = false;
    private boolean addRemarkComments1 = false;
    private SimpleDateFormat dateFormat1;

    @Override
    public void addConfigurationProperties(Properties properties) {
        this.properties1.putAll(properties);
        this.suppressDate1 = StringUtility.isTrue(properties.getProperty("suppressDate"));
        this.suppressAllComments1 = StringUtility.isTrue(properties.getProperty("suppressAllComments"));
        this.addRemarkComments1 = StringUtility.isTrue(properties.getProperty("addRemarkComments"));
        String dateFormatString = properties.getProperty("dateFormat");
        if (StringUtility.stringHasValue(dateFormatString)) {
            this.dateFormat1 = new SimpleDateFormat(dateFormatString);
        }

        // 为父类相同字段赋值
        try {
            // 得到私有字段
            java.lang.reflect.Field propertiesField = DefaultCommentGenerator.class.getDeclaredField("properties");
            // 通过反射设置私有对象可以访问
            propertiesField.setAccessible(true);
            // 将私有对象设置新的值
            propertiesField.set(this, properties1);

            java.lang.reflect.Field suppressDateField = DefaultCommentGenerator.class.getDeclaredField("suppressDate");
            suppressDateField.setAccessible(true);
            suppressDateField.set(this, suppressDate1);

            java.lang.reflect.Field suppressAllCommentsField = DefaultCommentGenerator.class.getDeclaredField("suppressAllComments");
            suppressAllCommentsField.setAccessible(true);
            suppressAllCommentsField.set(this, suppressAllComments1);

            java.lang.reflect.Field addRemarkCommentsField = DefaultCommentGenerator.class.getDeclaredField("addRemarkComments");
            addRemarkCommentsField.setAccessible(true);
            addRemarkCommentsField.set(this, addRemarkComments1);

            if (StringUtility.stringHasValue(dateFormatString)) {
                java.lang.reflect.Field dateFormatField = DefaultCommentGenerator.class.getDeclaredField("dateFormat");
                dateFormatField.setAccessible(true);
                dateFormatField.set(this, dateFormat1);
            }

        } catch (Exception e) {

        }
    }


    @Override
    public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
        field.addJavaDocLine("/**");
        // 优化输出格式，存在换行符则换行显示
        String remarks = introspectedColumn.getRemarks();
        if (remarks != null) {
            String[] split = remarks.split("\n|\r");
            for (String s : split) {
                if (!"".equals(s.trim())) {
                    field.addJavaDocLine(" * " + s);
                }
            }
        }
        field.addJavaDocLine(" */");
    }

    @Override
    public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {

        topLevelClass.addJavaDocLine("/**");
        String remarks = introspectedTable.getRemarks();
        StringBuilder sb = new StringBuilder();
        sb.append(" * ");
        sb.append(introspectedTable.getFullyQualifiedTable());
        sb.append(" ");
        sb.append(remarks);
        topLevelClass.addJavaDocLine(sb.toString());
        topLevelClass.addJavaDocLine(" *");

        StringBuilder sb1 = new StringBuilder();
        sb1.append(" * ");
        sb1.append("@author");
        sb1.append(" ");
        sb1.append("todo");
        topLevelClass.addJavaDocLine(sb1.toString());

        StringBuilder sb2 = new StringBuilder();
        sb2.append(" * ");
        sb2.append("@date");
        sb2.append(" ");
        sb2.append(super.getDateString());
        topLevelClass.addJavaDocLine(sb2.toString());

        topLevelClass.addJavaDocLine(" */");
    }


}
